package syf.doublelist;

public class DoubleLinkList {
    private HeroNode head = new HeroNode(null, "", "");

    public DoubleLinkList() {
    }

    public DoubleLinkList(HeroNode head) {
        this.head = head;
    }

    public void addNode(HeroNode node) {
        HeroNode temp = head;
        while (true) {
            if (null == temp.next) {
                temp.next = node;
                node.pre = temp;
                break;
            }
            if (temp.no == node.no)
                break;
            temp = temp.next;
        }
    }

    public void Print() {
        HeroNode temp = head;
        while (true) {
            if (temp.next == null)
                break;
            System.out.println(temp.next);
            temp = temp.next;
        }
    }

    //修改节点,编号不能改
    public void Modify(HeroNode heroNode) {
        HeroNode temp = head;
        while (true) {
            if (null == temp.next) {
                System.out.println("没有该节点信息");
                break;
            }
            if (temp.next.no == heroNode.no) {
                temp.next.name = heroNode.name;
                temp.next.NikeName = heroNode.NikeName;
                break;
            } else {
                temp = temp.next;
            }
        }
    }

    public void Delete(HeroNode heroNode) {
        HeroNode temp = head;
        while (true) {
            if (null == temp.next) {
                System.out.println("没有该节点信息");
                break;
            }
            temp = temp.next;
            if (temp.no.equals(heroNode.no)){
                temp.pre.next=temp.next;
                if (temp.next!=null)
                    temp.next.pre=temp.pre;
                break;
            }
        }
    }
    //有序添加节点
    public void addNodeByOrder(HeroNode heroNode) {
        HeroNode temp = head;
        while (true) {
            if (null == temp.next) {
                temp.next=heroNode;
                heroNode.pre=temp;
                break;
            }
            else {
                if (temp.next.no>heroNode.no){
                    heroNode.next=temp.next;
                    temp.next.pre=heroNode;
                    temp.next = heroNode;
                    heroNode.pre=temp;
                    break;
                }
                else if (temp.next.no==heroNode.no) {
                    System.out.println("编号已存在，不能添加");
                    break;
                }
                else temp = temp.next;
            }
        }
    }
}
